package controller;
import java.awt.Toolkit;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

/**
 * @author Alexander Müller
 * 
 */
public class BinaryIpDocument extends PlainDocument {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void insertString(int offset, String s, AttributeSet a) {

		try {
			if (s.length() == 1) {
			if (offset > 7) {
				s = null;
			}
			if (Integer.parseInt(s) > 1) {
				throw new NumberFormatException();
			}
			Integer.parseInt(s);
			}

		} catch (NumberFormatException e) {

			Toolkit.getDefaultToolkit().beep();
			return;
		}

		try {
			super.insertString(offset, s, a);
		} catch (BadLocationException e) {

			e.printStackTrace();
		}
	}

}
